package com.icss.train_sys_back.controller.admin.system_manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.system_manage.MenuEntity;
import com.icss.train_sys_back.service.system_service.MenuManageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.util.Introspection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小泽
 * @createTime 2025/9/29 14:22
 */
@RestController
@RequestMapping("/admin/menu_manage")
@Slf4j
public class MenuManageController {
    @Autowired
    private MenuManageService menuManageService;

    //获取所有菜单列表
    @GetMapping("/getAllMenus")
    public ResponseResult getAllMenus() {
        log.info("获取所有菜单列表");
        List<MenuEntity> menuEntityList = menuManageService.getAllMenus();
        return ResponseResult.success(menuEntityList);
    }

    //分页+条件查询菜单列表
    @GetMapping("/getList")
    public ResponseResult getListConditions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "8") Integer pageSize,
            @RequestParam(required = false) String menuName,
            @RequestParam(required = false) String menuUrl,
            @RequestParam(required = false) Integer parentId,
            @RequestParam(required = false) Integer isShow
    ) {
        log.info("菜单列表分页+条件查询 page={},pageSize={},menuName={},menuUrl={},parentId={},isShow={}",
                page, pageSize, menuName, menuUrl, parentId, isShow);
        PageHelper.startPage(page, pageSize);
        List<MenuEntity> menuEntityList = menuManageService.getListConditions(menuName, menuUrl, parentId, isShow);
        PageInfo<MenuEntity> pageInfo = new PageInfo<>(menuEntityList);
        return ResponseResult.success(pageInfo);
    }

    //菜单添加
    @PostMapping("/add")
    public ResponseResult add(@RequestBody MenuEntity menu) {
        log.info("菜单添加 menu = {}", menu);
        menuManageService.add(menu);
        return ResponseResult.success();
    }

    //菜单更新
    @PostMapping("/update")
    public ResponseResult update(@RequestBody MenuEntity menu) {
        log.info("菜单更新 menu = {}", menu);
        menuManageService.update(menu);
        return ResponseResult.success();
    }

    //菜单启用停用状态管理
    @PostMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody MenuEntity menu) {
        log.info("车站状态改变 menuId = {} , status = {}", menu.getMenuId(), menu.getIsShow());
        menuManageService.changeStatus(menu.getMenuId(), menu.getIsShow());
        return ResponseResult.success();
    }

    //根据menuId 删除菜单
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody MenuEntity menu) {
        log.info("根据menuId 删除菜单  menuId = {}", menu.getMenuId());
        menuManageService.delete(menu.getMenuId());
        return ResponseResult.success();
    }

    //根据RoleId 获取菜单列表进行菜单加载
    @GetMapping("/getMenusByRoleId")
    public ResponseResult getMenusByRoleId(@RequestParam Integer roleId) {
        log.info("根据RoleId 获取菜单列表进行菜单加载 roleId = {}", roleId);
        List<MenuEntity> menuEntityListByRoleId = menuManageService.getMenusByRoleId(roleId);
        return ResponseResult.success(menuEntityListByRoleId);
    }
}
